//
//  HS_BigCollectionViewCell.m
//  悦享
//
//  Created by 韩双 on 16/3/17.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HS_BigCollectionViewCell.h"
#import <MJRefresh.h>

@interface HS_BigCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *allDetailModelArr;  /**< 总数据数组 */
@property (nonatomic, retain) NSMutableArray *footerDataArr;  /**< 尾视图数据 */
@property (nonatomic, assign) NSInteger flagSection; /** 记录正在播放视频cell-> section */
@property (nonatomic, assign) BOOL isPlaying; /** 记录视频播放状态 */
@property (nonatomic, assign) BOOL playIsOrNo;   /**< 判断播放 */

@property (nonatomic, assign) BOOL refreshIsOrNo;  /**< 判断刷新或加载 */


@end

@implementation HS_BigCollectionViewCell

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"refresh" object:nil];
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {

        self.allDetailModelArr = [NSMutableArray array];
        self.footerDataArr = [NSMutableArray array];
        
        [self createSubviews];
        _isPlaying = YES;
        _playIsOrNo = YES;
        
    }
    return self;
    
}

- (void)setHttp:(NSString *)http {
    
    _http = http;

}

- (void)createSubviews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SREEN_WIDTH , self.frame.size.height - 114) collectionViewLayout:layout];
    
    [self.contentView addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[HS_DetailCollectionViewCell class] forCellWithReuseIdentifier:@"HS_DetailCollectionViewCell"];
    [_collectionView registerClass:[HS_DetailCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView registerClass:[HS_DetailFooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    [self addHeader];
    [self addFooder];
    

}

#pragma mark - 创建加载与刷新

- (void)addHeader {
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        _refreshIsOrNo = YES;
        
        [self dataHundle];
        
    }];

    _collectionView.mj_header = header;
    
}

- (void)addFooder {
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
       
        _refreshIsOrNo = NO;
        [self dataHundle];
        
    }];
    
    _collectionView.mj_footer = footer;
    
}



#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 1;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HS_DetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HS_DetailCollectionViewCell" forIndexPath:indexPath];
    HS_detailModle *model = _allDetailModelArr[indexPath.section];
    
    cell.model = model;
    
    [cell.startButton addTarget:self action:@selector(startButtonAcrion:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.fullButton addTarget:self action:@selector(fullButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.tag = 1000 + indexPath.section;
    cell.startButton.tag = 1500 + indexPath.section;
    
    cell.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];

    return cell;
    
}


// 头尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        
        HS_DetailCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        HS_detailModle *model = _allDetailModelArr[indexPath.section];
        
        header.model = model;
        
        return header;
    }else{
        
        HS_DetailFooterCollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footer.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
        
        HS_commentModel *model = _footerDataArr[indexPath.section][0];
        footer.model = model;
        return footer;
    }

    
    
}

// layout的itemSize
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HS_detailModle *model = _allDetailModelArr[indexPath.section];
    
    if (model.video_url) {
        
        NSInteger height = (SREEN_WIDTH - 20) * [model.video_height integerValue] / [model.video_width integerValue];
        
        return CGSizeMake(SREEN_WIDTH - 20, height);
    }
    if (model.mp4_url){
        
        NSInteger height = (SREEN_WIDTH - 20) * [model.gif_height integerValue] / [model.gif_width integerValue];
        
        return CGSizeMake(SREEN_WIDTH - 20, height + 10);
    }
    return CGSizeMake(SREEN_WIDTH - 20, 0);
    
}
// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return _allDetailModelArr.count;
}

// 头视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    HS_detailModle *model = _allDetailModelArr[section];
    
     CGSize size = [APPTools labelRectWithSize:CGSizeMake(SREEN_WIDTH - 20, MAXFLOAT) LabelText:model.content Font:[UIFont systemFontOfSize:17]];
    
    return CGSizeMake(size.width, size.height + 50 + 40);

}

// 尾视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    NSArray *arr = _footerDataArr[section];
    
    if (arr.count > 0) {

        HS_commentModel *model = _footerDataArr[section][0];

        CGSize size1 = [APPTools labelRectWithSize:CGSizeMake(SREEN_WIDTH, MAXFLOAT) LabelText:model.text Font:[UIFont systemFontOfSize:16]];

        // 第一个50 人物头像姓名高度  60 底部收藏喜欢等View + 中间灰色部分
        return CGSizeMake(SREEN_WIDTH, 50 + size1.height + 20);
        
    }
    
    return CGSizeMake(SREEN_WIDTH, 0);
    
}


#pragma mark - cell上全屏按钮的点击方法
- (void)fullButtonAction:(UIButton *)button {
    
//    HS_DetailCollectionViewCell *cell = [_collectionView viewWithTag:button.tag - 500];
    
    if ([UIDevice currentDevice].orientation != UIDeviceOrientationPortrait) {
        // 竖屏
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
        
    }else{
        // 横屏
        NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
        [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
    }
    
}

#pragma mark - cell上播放按钮的点击方法
- (void)startButtonAcrion:(UIButton *)button {
    
    HS_DetailCollectionViewCell *cell1 = [_collectionView viewWithTag:button.tag - 501];
    HS_DetailCollectionViewCell *cell2 = [_collectionView viewWithTag:button.tag - 500];
    HS_DetailCollectionViewCell *cell3 = [_collectionView viewWithTag:button.tag - 499];
    
    if (_isPlaying) {
        [cell1.player pause];
        [cell2.player play];
        [cell3.player pause];
        
        [cell1.startButton setImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateNormal];
        [cell2.startButton setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:UIControlStateNormal];
        _isPlaying = YES;
    }
    else
    {
        [cell2.player play];
        [cell1.startButton setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:UIControlStateNormal];
        
        _isPlaying = NO;
    }
    if (_playIsOrNo) {
        
        [cell2.player play];
        
        [cell2.startButton setImage:[UIImage imageNamed:@"iconfont-zanting"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:2 animations:^{
            
            button.alpha = 0;
            cell2.endView.alpha = 0;
            _playIsOrNo = !_playIsOrNo;
        }];
    }else{
        
        [cell2.player pause];
        
        [cell2.startButton setImage:[UIImage imageNamed:@"iconfont-bofang"] forState:UIControlStateNormal];
        _playIsOrNo = !_playIsOrNo;
        
    }
    
}


- (void)dataHundle {
    
//    NSLog(@"%@", _http);
    
    [APPTools GETWithURL:_http par:nil success:^(id responseObject) {
        
        if (_refreshIsOrNo) {
            
            [_allDetailModelArr removeAllObjects];
            [_footerDataArr removeAllObjects];
            
        }
        
        NSArray *result = responseObject[@"data"][@"data"];
        
        for (NSDictionary *dic in result) {
            
            HS_detailModle *model = [HS_detailModle baseModelWithDic:dic[@"group"]];
            
            [_allDetailModelArr addObject:model];
            
            // 尾视图
            NSMutableArray *arr = [NSMutableArray array];
            
            for (NSDictionary *data in dic[@"comments"]) {
                
                HS_commentModel *model = [HS_commentModel baseModelWithDic:data];
                [arr addObject:model];
            }
            
            [_footerDataArr addObject:arr];
            
        }
        
        
        
        [_collectionView reloadData];
        
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil];

    } filed:^(NSError *error) {
        
    }];
    
    
}




@end
